
global M_growth = "growth_pwt"
global M_growth_MA12 = "growthM12_pwt"
global M_growth_MA123 = "growthM123_pwt"
global M_recovery = "recovery_pwt"
global M_recovery_MA123 = "recovery_pwt_MA123"
global M_recovery_MA12345 = "recovery_pwt_MA12345"
global M_recovery_Max123 = "recovery_pwt_MAX123"
global M_recovery_MAx12345 = "recovery_pwt_MAX12345"

global Tflow = "fdi_current_un_log"
global Tpc = "fdi_pc_un_log"
global Tgdp = "fdi_gdp_un"

global Y = "recur"

global M_control = "pwt101_hc_log_lag1 emp_pwt_log_lag1 trade_WDI_lag1 lnaid_lag1 outcome_govt intensity_war conflictdura pop_pwt_log gdppc_pwt_log_lag" 
global Y_control2 = "issue_territory pko outcome_govt intensity_war conflictdura pop_pwt_log gdppc_pwt_log_lag peaceyrs_conflict peaceyrs_conflict2 peaceyrs_conflict3"

global Inst = "inst_oecd_gdp_distance_log"
global Inst_UNSC = "inst_oecd_gdp_distance_UNSC_log"




******************************************************************
// Table 1
******************************************************************
use replication_data, clear  

// Start the program (the bootstrapping program is outside the loop)
capture program drop boot2sri
program boot2sri, rclass
	args varlistm varlistx
	reg `varlistx' $Inst $Y_control2 
	capture drop ehat
	predict ehat, res
	gsem (`varlistm' <- `varlistx' $M_control, family(gaussian) link(identity)) ($Y <- `varlistm' `varlistx' $Y_control2 ehat, family(binomial) link(probit)), vce(cluster conflict_id)
end

// making an empty word document
reg year
outreg2 using table1, noobs nocons replace word
asdoc sum year, save(table1_pci) replace

// Beginning of Loop 
foreach varlistm of varlist $M_recovery $M_recovery_MA123 $recovery_pwt_MA12345 $recovery_pwt_MAX123 $recovery_pwt_MAX12345{
	foreach varlistx of varlist $Tpc $Tgdp $Tflow {
		bootstrap X_M = _b[`varlistm':`varlistx'] ///
				M_Y = _b[$Y:`varlistm'] ///
				X_Y_Direct = _b[$Y:`varlistx'] ///
				X_Y_Indirect = (_b[`varlistm':`varlistx']*_b[$Y:`varlistm']) ///
				X_Y_Total = (_b[`varlistm':`varlistx']*_b[$Y:`varlistm']+_b[$Y:`varlistx']) ///
				RIT = ( (_b[`varlistm':`varlistx']*_b[$Y:`varlistm']) / ((_b[`varlistm':`varlistx']*_b[$Y:`varlistm'])+_b[$Y:`varlistx']) ), ///
				seed(999) reps(3000): boot2sri `varlistm' `varlistx'
		estat bootstrap, p
		mat b = e(b)', e(se)', e(ci_percentile)'
		asdoc wmat, mat(b) dec(3)  rnames(XM MY Direct Indirect Total RIT) cnames(Coef SE pc95%L pc95%U) append save(table1_pci.doc) title(Model_`varlistm'_`varlistx')	
		outreg2 using table1, dec(3) word
	}
}


// Note that Table 1 reports the results for the first two mediators: $M_recovery $M_recovery_MA123.
// The other mediators, $recovery_pwt_MA12345 $recovery_pwt_MAX123 $recovery_pwt_MAX12345, are also used in the program above to obtain the full results for Figure 2 below.
// The "table1_pci.doc" file is used to create "figure1_result_pci.dta." 



***************************************************************************************
// Figure 2: figure1_result_pci data were compiled using table1_pci and tableA2_pci
// Required packages: install "eclplot" and "coefplot" 
***************************************************************************************
use figure1_result_pci, replace

// Effect of FDI on Economic Recovery
eclplot coef lower upper x if effect==1, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Effect of FDI on Economic Recovery", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(xm, replace) 
	
// Effect of Recovery on Recurrence
eclplot coef lower upper x if effect==2, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) yscale(range(-0.007 0)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Effect of Economic Recovery on Recurrence", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(my, replace) 
		
// Direct Effect of FDI on Recurrence
eclplot coef lower upper x if effect==3, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Direct Effect of FDI on Recurrence", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(direct, replace) 
			
// Indirect Effect of FDI on Recurrence
eclplot coef lower upper x if effect==4, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Indirect Effect of FDI on Recurrence", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(indirect, replace) 
	
// Total Effect of FDI on Recurrence
eclplot coef lower upper x if effect==5, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Total Effect of FDI on Recurrence", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(total, replace) 
	
// Percentage of Indirect Effect
eclplot coef lower upper x if effect==6, ///
	ciopts(blcolor(black)) estopts(color(black)) xscale(range(0 16)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) yline(0, lpattern(dot) lwidth(thick)) ///
	ytitle("", size(1)) title("Percentage of Indirect Effect", size(3)) scheme(s1mono)  ///
	xlabel(1 "(1)PC"  2 "(1)GDP" 3 "(1)Flow" 4 "(2)PC"  5 "(2)GDP" 6 "(2)Flow" 7 "(3)PC"  8 "(3)GDP" 9 "(3)Flow" 10 "(4)PC"  11 "(4)GDP" 12 "(4)Flow" 13 "(5)PC"  14 "(5)GDP" 15 "(5)Flow", angle(vertical) labsize(small)) xtitle("") saving(rit, replace) 
	

graph combine xm.gph my.gph direct.gph indirect.gph total.gph rit.gph, saving(figure2, replace) 
	
	
	
































